﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Paef21")]
    public class Paef21Controller : ApiController
    {

        [HttpPost]
        [Route("PostPaef21_Inq")]
        public System.Data.DataSet PostPaef21_Inq([FromBody]  JObject obj)
        {
            EI.EIInfo inBlock = new EI.EIInfo();

            string company_code = obj["COMPANY_CODE"] == null ? " " : obj["COMPANY_CODE"].ToString();
            string branch_code  = obj["BRANCH_CODE"] == null ? " " : obj["BRANCH_CODE"].ToString();
            string stock_no = obj["STOCK_NO"] == null ? " " : obj["STOCK_NO"].ToString();
            string pa_location_no = obj["PA_LOCATION_NO"] == null ? " " : obj["PA_LOCATION_NO"].ToString();
            string asmat_no = obj["ASMAT_NO"] == null ? " " : obj["ASMAT_NO"].ToString();
            string asmat_name = obj["ASMAT_NAME"] == null ? " " : obj["ASMAT_NAME"].ToString();
            string spec = obj["SPEC"] == null ? " " : obj["SPEC"].ToString();
            string asmat_big_type = obj["ASMAT_BIG_TYPE"] == null ? " " : obj["ASMAT_BIG_TYPE"].ToString();
            string userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();

             string page_no = obj["PAGE_NO"] == null ? " " : obj["PAGE_NO"].ToString();

             string page_size = obj["PAGE_SIZE"] == null ? " " : obj["PAGE_SIZE"].ToString();

             string type_inq = obj["TYPE_INQ"] == null ? " " : obj["TYPE_INQ"].ToString();

            
           

            inBlock.Tables[0].Columns.Add("COMPANY_CODE");
            inBlock.Tables[0].Columns.Add("BRANCH_CODE");
            inBlock.Tables[0].Columns.Add("STOCK_NO"); //后台必须的字段；
            inBlock.Tables[0].Columns.Add("PA_LOCATION_NO");
            inBlock.Tables[0].Columns.Add("ASMAT_NO");
            inBlock.Tables[0].Columns.Add("ASMAT_NAME");
            inBlock.Tables[0].Columns.Add("SPEC");
            inBlock.Tables[0].Columns.Add("ASMAT_BIG_TYPE");



            inBlock.Tables[0].Rows.Add();
            inBlock.sys_info.company_code   = "LT";
            inBlock.sys_info.userid         = userid;


            inBlock.Tables[0].Rows[0]["COMPANY_CODE"] = company_code;
            inBlock.Tables[0].Rows[0]["BRANCH_CODE"] = branch_code;


            inBlock.Tables[0].Rows[0]["PA_LOCATION_NO"] = pa_location_no;
            inBlock.Tables[0].Rows[0]["STOCK_NO"] = stock_no;
            inBlock.Tables[0].Rows[0]["ASMAT_NO"] = asmat_no;
            inBlock.Tables[0].Rows[0]["ASMAT_NAME"] = asmat_name;


            inBlock.Tables[0].Rows[0]["SPEC"] = spec;
            inBlock.Tables[0].Rows[0]["ASMAT_BIG_TYPE"] = asmat_big_type;

 

            EF.DSUtility.PageInfoDataTable PageInfo = new EF.DSUtility.PageInfoDataTable();
            PageInfo.AddPageInfoRow((int.Parse(page_no) - 1) * int.Parse(page_size), int.Parse(page_size));

            inBlock.Tables.Add("PageInfo"); //后台需要
            inBlock.Tables["PageInfo"].Merge(PageInfo);
        
            string svr = "paef21_inq"; ////高级查询
            if (type_inq=="1")
            {
                svr = "paef21_inq2"; 
            }
                
            EI.EIInfo outBlock = EI.EITuxedo.CallService(svr, inBlock);










            outBlock.Tables[0].Columns.Add("STOCK_NO_DESC");
            outBlock.Tables[0].Columns.Add("ASMAT_BIG_TYPE_DESC");

            


            string sql_str1 = "select STOCK_NO CODE, STOCK_NAME CODE_DESC_1_CONTENT ,BRANCH_CODE  from tsimm01 where STOCK_TYPE='A'  ";

            //仓库代码转换
            EI.EIInfo outBlock_TEMP = EF.Utility.ExecQuery(sql_str1);


            //小代码转换；
            EI.EIInfo outBlock_TEMP2 = EF.Utility.GetCodeClassValue("DM41");
            

            DataTable dtTemp = null;


            for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
            { 
                string STOCK_NO = outBlock.Tables[0].Rows[i]["STOCK_NO"].ToString().Trim();
                if (STOCK_NO.Trim() != "")
                {
                    if (outBlock_TEMP.Tables[0].Select("CODE  = '" + STOCK_NO + "'").Count<DataRow>() == 1)
                    {
                        dtTemp = outBlock_TEMP.Tables[0].Select("CODE  = '" + STOCK_NO + "'").CopyToDataTable<DataRow>();
                        outBlock.Tables[0].Rows[i]["STOCK_NO_DESC"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                    }
                }
                string ASMAT_BIG_TYPE = outBlock.Tables[0].Rows[i]["ASMAT_BIG_TYPE"].ToString().Trim();
                if (ASMAT_BIG_TYPE.Trim() != "")
                {
                    if (outBlock_TEMP2.Tables[0].Select("CODE  = '" + ASMAT_BIG_TYPE + "'").Count<DataRow>() == 1)
                    {
                        dtTemp = outBlock_TEMP2.Tables[0].Select("CODE  = '" + ASMAT_BIG_TYPE + "'").CopyToDataTable<DataRow>();
                        outBlock.Tables[0].Rows[i]["ASMAT_BIG_TYPE_DESC"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                    }
                }
               
                  
            }










            outBlock.Tables[0].TableName = "Table0";









            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;


        }


        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】
        [HttpPost]
        [Route("PostPaef21_Load")]
        public System.Data.DataSet PostPaef21_Load([FromBody]  JObject obj)
        {

            //二级库维护记录中所有设计到的分公司
            string sql_str1  =  "select CODE,CODE_DESC_1_CONTENT from tep0002 where code_class = 'FIBR'   ";
            sql_str1        +=  "and code  in (select BRANCH_CODE from tsimm01 where STOCK_TYPE='A') ";
             
            EI.EIInfo outBlock = EF.Utility.ExecQuery(sql_str1);

            DataRow dr3 = outBlock.Tables[0].NewRow();
            dr3["CODE"] = " ";
            dr3["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables[0].Rows.InsertAt(dr3, 0);

            outBlock.Tables[0].TableName = "BRANCH_CODE";


            sql_str1 = " select distinct  T.STOCK_CHAR CODE ,tb.CODE_DESC_1_CONTENT ,T.BRANCH_CODE   ";
            sql_str1 +=  "  from  tsimm01 T ,(select CODE,CODE_DESC_1_CONTENT  from tep0002 where code_class='MM59' ) TB    ";
            sql_str1 +=  " where    T.STOCK_TYPE='A'     ";
            sql_str1 +=  " AND      T.STOCK_CHAR = TB.CODE    ";
            sql_str1 += " order     by T.BRANCH_CODE ,T.STOCK_CHAR     ";


            EI.EIInfo outBlock1 = EF.Utility.ExecQuery(sql_str1);
            DataRow dr4 = outBlock1.Tables[0].NewRow();
            dr4["CODE"] = " ";
            dr4["CODE_DESC_1_CONTENT"] = " ";
            outBlock1.Tables[0].Rows.InsertAt(dr4, 0);

            outBlock1.Tables[0].TableName = "STOCK_CHAR";





            //暂时不考虑二级库的仓库授权问题
            sql_str1 = "select STOCK_NO CODE, STOCK_NAME CODE_DESC_1_CONTENT ,BRANCH_CODE,STOCK_CHAR  from tsimm01 where STOCK_TYPE='A'  order by STOCK_NAME ";

            EI.EIInfo outBlock2 = EF.Utility.ExecQuery(sql_str1);
            //多项选择，不需要空行；
            //DataRow dr4 = outBlock2.Tables[0].NewRow();
            //dr4["CODE"] = " ";
            //dr4["CODE_DESC_1_CONTENT"] = " ";
            //outBlock2.Tables[0].Rows.InsertAt(dr4, 0);

            outBlock2.Tables[0].TableName = "STOCK_NO";



            //暂时不考虑二级库的仓库授权问题
            sql_str1 = "select distinct a.DPTMENT_CODE as CODE,b.CODE_DESC_1_CONTENT from tsifiaa71 a,tep0002 b  where  b.code_class='FIDP' and a.DPTMENT_CODE = b.code and b.code_desc_5_content in('2','3') order by a.dptment_code";
             
            EI.EIInfo outBlock3 = EF.Utility.ExecQuery(sql_str1);

            DataRow dr5 = outBlock3.Tables[0].NewRow();
            dr5["CODE"] = " ";
            dr5["CODE_DESC_1_CONTENT"] = " ";
            outBlock3.Tables[0].Rows.InsertAt(dr5, 0);

            outBlock3.Tables[0].TableName = "DPTMENT_CODE";




            outBlock.Tables.Add(outBlock1.Tables[0].Copy());
            outBlock.Tables.Add(outBlock2.Tables[0].Copy());
            outBlock.Tables.Add(outBlock3.Tables[0].Copy());  





            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        [HttpPost]
        [Route("PostPaef21_save")]

        //库位变更、类别变更
        public System.Data.DataSet PostPaef21_save([FromBody]  JObject obj)
        {


            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();


             

            inBlock.Tables[0].Columns.Add("COMPANY_CODE");
            inBlock.Tables[0].Columns.Add("ASMAT_NO");
            inBlock.Tables[0].Columns.Add("STOCK_NO");
            inBlock.Tables[0].Columns.Add("ASMAT_BIG_TYPE");
            inBlock.Tables[0].Columns.Add("ASMAT_BIG_NAME");
            inBlock.Tables[0].Columns.Add("ASMAT_SMALL_TYPE");
            inBlock.Tables[0].Columns.Add("ASMAT_SMALL_NAME");
            inBlock.Tables[0].Columns.Add("PA_LOCATION_NO");
            inBlock.Tables[0].Columns.Add("REMARK");
            inBlock.Tables[0].Columns.Add("USE_PLACE");
            inBlock.Tables[0].Columns.Add("DPTMENT_CODE");


            inBlock.Tables[0].Columns.Add("COMPANY_CNAME");

           
          

            inBlock.Tables[0].Rows.Add();
            inBlock.Tables[0].Rows[0]["COMPANY_CODE"] = obj["COMPANY_CODE"] == null ? " " : obj["COMPANY_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_NO"] = obj["ASMAT_NO"] == null ? " " : obj["ASMAT_NO"].ToString();
            inBlock.Tables[0].Rows[0]["STOCK_NO"] = obj["STOCK_NO"] == null ? " " : obj["STOCK_NO"].ToString();

            inBlock.Tables[0].Rows[0]["ASMAT_BIG_TYPE"] = obj["ASMAT_BIG_TYPE"] == null ? " " : obj["ASMAT_BIG_TYPE"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_BIG_NAME"] = obj["ASMAT_BIG_NAME"] == null ? " " : obj["ASMAT_BIG_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_SMALL_TYPE"] = obj["ASMAT_SMALL_TYPE"] == null ? " " : obj["ASMAT_SMALL_TYPE"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_SMALL_NAME"] = obj["ASMAT_SMALL_NAME"] == null ? " " : obj["ASMAT_SMALL_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["PA_LOCATION_NO"] = obj["PA_LOCATION_NO"] == null ? " " : obj["PA_LOCATION_NO"].ToString();
            inBlock.Tables[0].Rows[0]["REMARK"] = obj["REMARK"] == null ? " " : obj["REMARK"].ToString();
            inBlock.Tables[0].Rows[0]["USE_PLACE"] = obj["USE_PLACE"] == null ? " " : obj["USE_PLACE"].ToString();
            inBlock.Tables[0].Rows[0]["DPTMENT_CODE"] = obj["DPTMENT_CODE"] == null ? " " : obj["DPTMENT_CODE"].ToString();


            //修改之前的类别           
            inBlock.Tables[0].Rows[0]["COMPANY_CNAME"] = obj["COMPANY_CNAME"] == null ? " " : obj["COMPANY_CNAME"].ToString();


    



            string srv = obj["SRV"] == null ? " " : obj["SRV"].ToString();
          

            //块0为删除；
            //块1为新增
            //块2为修改
            EI.EIInfo inBlock2 = new EI.EIInfo();
            inBlock2.sys_info.company_code = "LT";
            inBlock2.sys_info.company_name = "龙腾特钢";

            inBlock2.sys_info.userid = obj["USERID"].ToString();
            inBlock2.sys_info.username = obj["USERNAME"].ToString();
            inBlock2.sys_info.formname = obj["FORMNAME"].ToString();


            //inBlock2.Tables.Add();
            //inBlock2.Tables.Add();

            //库位变更
            if (srv.Contains("pabmb1_save"))
            {
                //inBlock2.Tables.Add(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].Merge(inBlock.Tables[0].Copy());

                inBlock2.Tables[0].TableName = "INS_UPD";


            }

            //类别变更
            else if (srv.Contains("pa0006_save"))
            {
                //inBlock2.Tables.Add(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].Merge(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].TableName = "UPD_TYPE";
            }

            else if (srv.Contains("pa0006_upd1"))
            {
                //inBlock2.Tables.Add(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].Merge(inBlock.Tables[0].Copy());
                inBlock2.Tables[0].TableName = "UPD1";
                srv = "pa0006_save";
            }

          



            //STOCK_NO,ASMAT_NO,ASMAT_BIG_TYPE,ASMAT_SMALL_TYPE,ASMAT_BIG_NAME,ASMAT_SMALL_NAME
            EI.EIInfo outBlock = EI.EITuxedo.CallService(srv, inBlock2);


            //srv = "dcjl42_pass";
            //srv = "dcjl42_reback";


          






            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;

        }

    }
}